package com.woniuxy.boot.bankcard.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.boot.bankcard.bean.Bankcard;
import com.woniuxy.boot.bankcard.exception.BalanceIsNotAmpleException;
import com.woniuxy.boot.bankcard.exception.BankcardIsNullException;
import com.woniuxy.boot.bankcard.exception.WrongPasswordException;
import com.woniuxy.boot.bankcard.feign.DistributedIdFeign;
import com.woniuxy.boot.bankcard.mapper.BankcardMapper;
import com.woniuxy.boot.bankcard.service.BankcardService;
import com.woniuxy.boot.common.dto.OrderPayDTO;
import com.woniuxy.boot.common.vo.BankcardVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class BankcardServiceImpl extends ServiceImpl<BankcardMapper, Bankcard> implements BankcardService {
    @Autowired
    private BankcardMapper bankcardMapper;

    @Autowired
    private DistributedIdFeign distributedIdFeign;

    /**
     * 绑定银行卡,加密银行卡密码
     **/
    @Override
    public void bindBankcard(Bankcard bankcard) {
        //uuid生成盐
        String salt = UUID.randomUUID().toString().replace("-", "").substring(0, 16);
        // 密码使用 MD5 加密，并结合盐
        String newPassword = encrypt(bankcard.getPassword(), salt);
        //设置数据
        bankcard.setId(distributedIdFeign.get());
        bankcard.setBalance(new BigDecimal("20000000"));
        bankcard.setPassword(newPassword);
        bankcard.setSalt(salt);
        bankcard.setStatus("a");
        bankcardMapper.insert(bankcard);
    }

    /**
     * 根据用户id查询回所有的用户已绑定银行卡
     **/
    @Override
    public List<BankcardVO> getAllBankcardById(Long userId) {
        List<BankcardVO> bankcardVOList = new ArrayList<>();
        LambdaQueryWrapper<Bankcard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Bankcard::getUserId, userId);
        List<Bankcard> bankcards = bankcardMapper.selectList(lambdaQueryWrapper);
        for (Bankcard bankcard : bankcards) {
            BankcardVO bankcardVO = new BankcardVO();
            BeanUtils.copyProperties(bankcard, bankcardVO);
            bankcardVOList.add(bankcardVO);
        }
        return bankcardVOList;
    }

    /**
     * 查询回银行卡,执行扣款
     **/
    @Override
    public void payOrder(OrderPayDTO orderPayDTO) {
        // 根据银行卡的id和银行卡号查询回一张银行卡
        LambdaQueryWrapper<Bankcard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Bankcard::getId, orderPayDTO.getBankCardId());
        lambdaQueryWrapper.eq(Bankcard::getCardId, orderPayDTO.getCardId());
        Bankcard bankcard = bankcardMapper.selectOne(lambdaQueryWrapper);
        if(bankcard==null){
            throw new BankcardIsNullException("银行卡不存在,请选择有效的银行卡");
        }
        //比较输入密码(加密后)和查询回的密码是否一致
        String newPassword = encrypt(orderPayDTO.getPassword(), bankcard.getSalt());
        if(!newPassword.equals(bankcard.getPassword())){
            throw new WrongPasswordException("密码不正确");
        }
        //比较银行卡的余额是否足矣支付
        int i = orderPayDTO.getPayMoney().compareTo(bankcard.getBalance());
        if (i > 0) {
            System.out.println("支付金额大于余额");
            throw new BalanceIsNotAmpleException("余额不足");
        } else {
            System.out.println("支付金额小于余额");
            bankcard.setBalance(bankcard.getBalance().subtract(orderPayDTO.getPayMoney()));
            bankcardMapper.insertOrUpdate(bankcard);
        }


    }

    /**
     *加密方法
     **/
    private String encrypt(String password, String salt) {
        return SecureUtil.md5(password+salt);
    }


}
